package org.springframework.ai.openai.samples.helloworld;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.image.ImageOptions;
import org.springframework.ai.image.ImageOptionsBuilder;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/ai")
class AIController {

	private final ChatClient chatClient;

	@Resource
    OpenAiImageModel imageModel;

	AIController(ChatClient chatClient) {
		this.chatClient = chatClient;
	}



	@GetMapping("/chat")
	String chat(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {
		return chatClient.prompt()
				.user(message)
				.call()
				.content();
	}


	@GetMapping("/chat/role")
	String completion(@RequestParam(value = "message", defaultValue = "你认识高则喜么") String message) {
		return chatClient.prompt()
				.user(message)
				.call()
				.content();
	}

	@GetMapping("/chat/img")
	public String text2Image(@RequestParam(value = "message", defaultValue = "迪士尼风格的一只可爱的小狗") String messag) {
		ImageResponse call = imageModel.call(
				new ImagePrompt(messag, ImageOptionsBuilder.builder().withHeight(1024).withN(1).withWidth(1024).build())
		);
		return call.getResult().getOutput().getUrl();
	}

	public String chatModel() {
		return "gpt-3.5-turbo";
	}
}
